(function() {
    'use strict';

    /**
     * @ngdoc directive
     * @name webApp.directive:fuwuList
     * @description
     * # fuwuList 
     */
    angular.module('webApp')
        .directive('fuwuList', function() {
            return {
                restrict: 'E',
                templateUrl: "views/directives.fuwu.list.html",
                scope: {
                    type: '@',
                    callback: '&'
                },
                controller: ['$templateCache', '$rootScope', '$scope', '$compile', 'dataService', '$timeout', 'helper', function($templateCache, $rootScope, $scope, $compile, dataService, $timeout, helper) {
                    //请求数据
                    $scope.fuwu_count = 0;
                    $scope.page_size = 10;
                    $scope.pageData = function(pageNo) {
                        var query = {
                            content: {
                                page_no: pageNo ? pageNo : "1",
                                page_size: $scope.page_size,
                                version_sku: 1,
                            }
                        };

                        dataService.fuwuGet(query).then(function(r) {
                            $scope.fuwu_sku = r.version;
                            $scope.order_cycle = r.order_cycle;
                        });
                    }

                    $scope.changeCycle = function(version, $event) {
                        $scope.fuwu_version = version;
                        $scope.month = 0;
                        $scope.season = 0;
                        $scope.half_year = 0;
                        $scope.year = 0;
                        
                        $scope.fuwu_id = $scope.fuwu_sku[$scope.fuwu_version].fuwu_id;

                        var price = $scope.fuwu_sku[$scope.fuwu_version][$scope.cycle];
                        $scope.fuwu_price = price;

                        //判断价格是否有0
                        var month = $scope.fuwu_sku[$scope.fuwu_version].month;
                        var season = $scope.fuwu_sku[$scope.fuwu_version].season;
                        var half_year = $scope.fuwu_sku[$scope.fuwu_version].half_year;
                        var year = $scope.fuwu_sku[$scope.fuwu_version].year;

                        if (month == 0) {
                            $scope.month = 1;
                        }

                        if(season == 0){
                            $scope.season = 1;
                        }

                        if(half_year == 0){
                            $scope.half_year = 1;
                        }

                        if(year == 0){
                            $scope.year = 1;
                        }

                        var className = $($event.currentTarget).attr('class');
                        if (className.indexOf('btn-bordered') >= 0) {
                            $($event.currentTarget).removeClass("btn-bordered");
                        } else {
                            $($event.currentTarget).addClass("btn-bordered");
                            $scope.fuwu_version = '';
                            $scope.fuwu_price = 0;
                            $scope.fuwu_id = 0;
                        }
                        $($event.currentTarget).siblings().addClass("btn-bordered");
                    }

                    $scope.showPrice = function(cycle, $event) {
                        if (!$scope.fuwu_version) {
                            helper.myAlert("请先选择服务版本！");
                            return false;
                        }

                        var price = $scope.fuwu_sku[$scope.fuwu_version][cycle];
                        $scope.fuwu_price = price;
                        $scope.cycle = cycle;

                        var className = $($event.currentTarget).attr('class');
                        if (className.indexOf('btn-bordered') >= 0) {
                            $($event.currentTarget).removeClass("btn-bordered");
                        } else {
                            $($event.currentTarget).addClass("btn-bordered");
                            $scope.fuwu_price = 0;
                            $scope.cycle = '';
                        }
                        $($event.currentTarget).siblings().addClass("btn-bordered");
                    }

                    if ($scope.type == "all") {
                        $scope.fuwu_type = "all";
                        $scope.pageData();
                    } else if ($scope.type == "alone") {
                        $scope.fuwu_type = "alone";
                    }
                }]
            }
        })
})();